'use strict';
	angular.module('inventory.fileStorageSelectSingle', ['app.services'])

	.controller('fileStorageSelectSingleCtrl'
			,['$rootScope', '$scope', 'BizData', '$uibModalInstance','params', 'title'
			   , f])//控制器结束
    function f($rootScope, $scope, BizData, $uibModalInstance, params, title) {

   		var Model = this;
		
   		
		Model.dataMasterAfterRefresh = function(event) {
			//console.log('dataMasterAfterRefresh.event-->>',event);
		};
   		
   		var options = {};
		options.container = Model;
		options.concept = 'FileStorage';
		options.name = 'dataMaster';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
   		options.creater = '/inventory/fileStorage/tree/create';
   		options.reader = '/inventory/fileStorage/tree/query';
   		options.writer = '/inventory/fileStorage/tree/save';
   		options.deleter = '/inventory/fileStorage/tree/delete';
   		options.idColumnDefine = "fID";
   		options.idColumnName = "fID";
   		options.isTree = true;
   		options.parentRelation = 'fParent';
   		options.nodeKindRelation = 'nodeKind';
   		options.nodeLevelRelation = 'nodeLevel';
   		options.nodeSortRelation = "sortNo";
   		options.rootFilter = 'FileStorage.fParent is null';
   		options.pathRelation = 'storageFullID';
   		options.directDeleteMode = false;
   		options.limit = -1;
   		options.orderBy = 'sortNo ASC';
   		
   		options.onAfterRefresh = Model.dataMasterAfterRefresh;
  		

   		Model.dataMaster = new BizData(options);
   		//监视对象中的所有变动
//   	Model.dataMaster.deregRows = $scope.$watch('Model.dataMaster.rows',Model.dataMaster.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
   		//////////////////////////////////////////////
		
		//////////////////////对话框使用开始//////////////////////////////////////
		
        //模态窗口打开之后执行的事件
        $uibModalInstance.opened.then(function(){
    		//接收调用对话框的参数
            Model.title = title;  
            Model.params = params;
            
        	//公司
    		//设置过滤条件
    		Model.dataMaster.filter = "FileStorage.billType='CKDA'";
    		Model.dataMaster.filter = Model.dataMaster.filter + " AND FileStorage.enterpriseNo='"+ Model.params.id +"'";
    		
            if(Model.params.storageID!=undefined && Model.params.storageID!=''){
        		Model.dataMaster.filter = Model.dataMaster.filter + " AND FileStorage.storageFullID LIKE'%"+ Model.params.storageID +"%'";//
            }          

            if(Model.params.factoryID!=undefined && Model.params.factoryID!=''){
        		Model.dataMaster.filter = Model.dataMaster.filter + " AND FileStorage.factoryID='"+ Model.params.factoryID +"'";//
            }          
            if(Model.params.storageType){
            	Model.dataMaster.filter = Model.dataMaster.filter + " AND FileStorage.storageType='"+ Model.params.storageType +"'"	;
            }

//          console.log('Model.params.storageType-->',Model.params.storageType);
     	
       		Model.dataMaster.refreshData();

        });  
		
        Model.ok = function () {  
	    	//保存成功
	    	//打包数据
	    	var event = {};
	    	var data = [];
	    	var sourceRow = Model.dataMaster.rows[Model.dataMaster.currentIndex];
	    	var row = {};
			//循环行中的列，判断变化点是哪一个
			for(var obj in sourceRow){
				if(obj!='$$hashKey' && obj!='userdata'){
					row[obj] = sourceRow[obj].value;
				}
			}
	    	
	    	data.push(row);
	    	event.data = data;
			$uibModalInstance.close(event);  
	    };  
	    
	    Model.cancel = function () {  
	        $uibModalInstance.dismiss('cancel');  
	    };    
		//////////////////////对话框使用结束//////////////////////////////////////
	    



	    return Model;
	    
	}	
	
